#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Capacitor钩子脚本：在同步后自动修复Java版本
function fixJavaVersionAfterSync() {
    console.log('🔧 Running after_sync hook: Fixing Java version...');
    
    const filesToFix = [
        'android/app/capacitor.build.gradle',
        'android/capacitor-cordova-android-plugins/build.gradle'
    ];
    
    let fixedCount = 0;
    
    filesToFix.forEach(filePath => {
        const fullPath = path.join(__dirname, '..', filePath);
        
        if (fs.existsSync(fullPath)) {
            let content = fs.readFileSync(fullPath, 'utf8');
            
            // 替换Java 21为Java 17
            const originalContent = content;
            content = content.replace(/JavaVersion\.VERSION_21/g, 'JavaVersion.VERSION_17');
            
            if (content !== originalContent) {
                fs.writeFileSync(fullPath, content);
                console.log(`✅ Fixed Java version in ${filePath}`);
                fixedCount++;
            }
        }
    });
    
    if (fixedCount > 0) {
        console.log(`🎉 Fixed Java version in ${fixedCount} file(s)`);
    } else {
        console.log('✅ All Java versions are already correct');
    }
}

// 运行修复
fixJavaVersionAfterSync();
